<?php

namespace App\Admin\Controllers;

use App\model\agent\AgentDepartment;
use App\model\customer\CustomerBigDepart;
use App\model\customer\CustomerSecondDepart;
use App\User;
use App\model\UserRole;

use Encore\Admin\Actions\Action;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Form\Builder;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class UsersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->column('name', __('用户名'));
        $grid->column('email', __('邮箱'));
        $states = [
                0=>'未设置',
                1=>'采购员',
                2=>'推广员',
                3=>'业务员',
        ];
        $grid->role_id('所属角色')->select($states);
        $grid->role('客户角色')->display(function($value ){
            $user= User::find($this->id);
            $roles=$user->getRoleNames();
            $name='';
            if ($roles->isEmpty()){
                return '未设置';
            }
            foreach ($roles as $v){
                $name.="<span class='label label-success'>$v</span>&nbsp";
            }
            return $name;
        });
        $grid->column('status', __('状态'))->using([

            1 => '已激活',
            2 => '已禁用',
            4 => '未激活',
        ], '未知')->dot([
            1 => 'success',
            2 => 'danger',
            4 => 'info',
        ], 'warning');
        $grid->column('qr_code', __('个人二维码'))->image('',50,50);
        $grid->column('type', __('类型'))->display(function($value){
            if($value == 0){
                return '企业微信';
            }else{
                return '小程序';
            }
        })->label([
            1 => 'default',
            0 => 'info',
        ]);
        $grid->column('gender', __('性别'))->display(function($value){
            if($value == 2){
                return '女';
            }else{
                return '男';
            }
        })->label([
            1 => 'danger',
            2 => 'info',
        ]);

        $grid->column('mobile', __('电话'));
        $grid->column('avatar', __('头像'))->image('',50,50);
        $grid->column('position', __('职位'));
        $grid->column('is_leader_in_dept', __('部门'))->display(function($value){
            return AgentDepartment::find($value)->name ?? '';
        });
        $grid->column('created_at', __('创建时间'));
        // 全部关闭

        $grid->actions(function(Grid\Displayers\Actions $action){
            $action->disableDelete();
            $action->disableView();
        });

        //禁用创建
        $grid->disableCreateButton();
        $grid->filter(function (Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->like('name','用户名');
            $filter->like('mobile','手机号');
        });
        return $grid;
    }



    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('email', __('Email'));
        $show->field('email_verified_at', __('Email verified at'));
        $show->field('password', __('Password'));
        $show->field('remember_token', __('Remember token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('role_id', __('Role id'));
        $show->field('userid', __('Userid'));
        $show->field('openid', __('Openid'));
        $show->field('status', __('Status'));
        $show->field('qr_code', __('Qr code'));
        $show->field('type', __('Type'));
        $show->field('gender', __('Gender'));
        $show->field('mobile', __('Mobile'));
        $show->field('avatar', __('Avatar'));
        $show->field('position', __('Position'));
        $show->field('is_leader_in_dept', __('Is leader in dept'));
        $show->field('session_key', __('Session key'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);
        $form->select('auth_map','数据权限')->options(\App\User::$authMap)->when(User::VIEW_CHILDREN,function (Form $form){
           $select= $form->multipleSelect('big_groups', '选择管理组')
               ->options(CustomerBigDepart::all()->pluck('name','id'));
            $arr=CustomerBigDepart::all()->pluck('id')->toArray();
            for ($i=1;$i<=count($arr);$i++){
                $shuzu=$this->combination($arr,$i);
                foreach ($shuzu as $item){
                    $select= $select->when($item,function (Form $form)use ($item){
                        $form->multipleSelect('groups','选择小组')->options(CustomerSecondDepart::whereIn('parent_id',$item)->get()->pluck('name','id'));
                    });
                }
            }

        });

        $form->multipleSelect('roles','角色')->options(Role::all()->pluck('name', 'id'));
        $form->multipleSelect('permissions','额外权限')->options(Permission::all()->pluck('cn_name', 'id'))->help('可以单独设定用户设定的权限');

        return $form;
    }
    function combination($a, $m) {
        $r = array();

        $n = count($a);
        if ($m <= 0 || $m > $n) {
            return $r;
        }

        for ($i=0; $i<$n; $i++) {
            $t = array($a[$i]);
            if ($m == 1) {
                $r[] = $t;
            } else {
                $b = array_slice($a, $i+1);
                $c = $this->combination($b, $m-1);
                foreach ($c as $v) {
                    $r[] = array_merge($t, $v);
                }
            }
        }

        return $r;
    }
}
